package com.cdtu.labpreview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.controller.vo.InstrumentVo;
import com.cdtu.labpreview.controller.vo.PictureDisplayVo;
import com.cdtu.labpreview.entity.Class;
import com.cdtu.labpreview.entity.InstrumentIntroduction;
import com.cdtu.labpreview.entity.PictureDisplay;
import com.cdtu.labpreview.entity.Student;
import com.cdtu.labpreview.mapper.ClassMapper;
import com.cdtu.labpreview.mapper.PictureDisplayMapper;
import com.cdtu.labpreview.mapper.StudentMapper;
import com.cdtu.labpreview.service.IPictureDisplayService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PictureDisplayServiceImpl extends ServiceImpl<PictureDisplayMapper, PictureDisplay> implements IPictureDisplayService {

    @Autowired
    private PictureDisplayMapper pictureDisplayMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassMapper classMapper;
    @Override
    public Result getPictureDisplay(int no, int type) {
        if(type==1){//学生
            //根据学号获取班级id
            LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Student::getSno,no);
            Student student = studentMapper.selectOne(lqw);
            long classId = student.getClassId();
            //根据班级id获取对应老师发布的图片展示
            LambdaQueryWrapper<PictureDisplay> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(PictureDisplay::getClassId,classId);
            List<PictureDisplay> pictureDisplays = pictureDisplayMapper.selectList(lqw2);
            List<PictureDisplayVo> collect = pictureDisplays.stream().map(pictureDisplay -> {
                String pic = pictureDisplay.getPictures();
                String[] pictureList=null;
                if(pic!=null){
                    pictureList = pic.split(",");
                }

                PictureDisplayVo iv = new PictureDisplayVo();
                BeanUtils.copyProperties(pictureDisplay, iv);
                iv.setPictureList(pictureList);
                return iv;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }else {//老师
            LambdaQueryWrapper<PictureDisplay> lqw=new LambdaQueryWrapper<>();
            lqw.eq(PictureDisplay::getTno,no);
            List<PictureDisplay> pictureDisplays = pictureDisplayMapper.selectList(lqw);
            List<PictureDisplayVo> collect = pictureDisplays.stream().map(pictureDisplay -> {
                long classId = pictureDisplay.getClassId();
                Class aClass = classMapper.selectById(classId);
                String cname = aClass.getName();

                String pic = pictureDisplay.getPictures();
                String[] pictureList=null;
                if(pic!=null){
                    pictureList = pic.split(",");
                }

                PictureDisplayVo iv = new PictureDisplayVo();
                BeanUtils.copyProperties(pictureDisplay, iv);
                iv.setClassName(cname);
                iv.setPictureList(pictureList);
                return iv;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }
    }

    @Override
    public Result addPictureDisplay(PictureDisplay pictureDisplay) {
        int flag = pictureDisplayMapper.insert(pictureDisplay);
        if(flag<=0) return Result.fail("添加失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result updatePictureDisplay(PictureDisplay pictureDisplay) {
        int flag = pictureDisplayMapper.updateById(pictureDisplay);
        if(flag<=0) return Result.fail("修改失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result deletePictureDisplay(long id) {
        int flag = pictureDisplayMapper.deleteById(id);
        if(flag<=0) return Result.fail("删除失败！请联系管理员");
        return Result.ok();
    }
}
